﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Threading;
using System.Windows.Data;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;

namespace GSSoftSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class TruongViewModel : ViewModelBase
    {
        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }
        private GSSoftContext _dc;

        /// <summary>
        /// Initializes a new instance of the TruongViewModel class.
        /// </summary>
        public TruongViewModel()
        {
            Init();
        }

        private void Init()
        {
            _dc = _dc = new GSSoftContext();//(GSSoftContext) context;
            IsBusy = true;
            _dc.Load<Truong>(_dc.GetTruongsQuery(), LoadBehavior.RefreshCurrent, LoadTruong_Completed, null);
        }

        private void LoadTruong_Completed(LoadOperation<Truong> load)
        {
            Truongs = new PagedCollectionView(new EntityList<Truong>(_dc.Truongs, load.Entities));
            IsBusy = false;
        }

        private void Complete(object sender, EventArgs e)
        {
            IsBusy = false;
        }

        private PagedCollectionView _truongs;
        public PagedCollectionView Truongs
        {
            get { return _truongs; }
            set
            {
                _truongs = value;
                RaisePropertyChanged("Truongs");
            }
        }

        /*{
            get
            {
                return new PagedCollectionView(new[]
                                                   {
                                                       new Truong(){MaTruong = "001", TenTruong = "001"},
                                                       new Truong(){MaTruong = "002", TenTruong = "002"},
                                                   });
            } 
            set
            {
                
            }
        }*/

        
        private Truong _current = null;
        public Truong Current
        {
            get
            {
                return _current;
            }
            set
            {
                if (_current == value)
                {
                    return;
                }
                _current = value;
                if (_current != null && (_current.EntityState == EntityState.New || _current.MaTruong == null))
                    _current.MaTruong = "";
                RaisePropertyChanged("Current");
            }
        }
        //public Truong Current { get; set; }

        private RelayCommand<object> _saveCommand;
        public RelayCommand<object> SaveCommand
        {
            get
            {
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand<object>(
                                          o =>
                                          {
                                              if (Current == null || !_dc.HasChanges || _dc.IsSubmitting)
                                                  return;
                                              ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                          },
                                          o => !(Current == null || !_dc.HasChanges || _dc.IsSubmitting)));
            }
        }

        private void Save()
        {
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            IsBusy = true;
            _dc.SubmitChanges(SubmitChangesEnd, null);
        }

        //public RelayCommand<object> SaveCommand { get; private set; }

        private void SaveChanges(object obj)
        {
            if (Current == null || !_dc.HasChanges)
                return;
            if (Current.EntityState == EntityState.New)
            {
                Current.NgayTao = Current.NgayCapNhat = DateTime.Now;
                Current.NguoiTao = Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (Current.EntityState == EntityState.Modified)
            {
                Current.NgayCapNhat = DateTime.Now;
                Current.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            if (!_dc.IsSubmitting)
            {
                IsBusy = true;
                _dc.SubmitChanges(SubmitChangesEnd, null);
            }
        }

        private void SubmitChangesEnd(SubmitOperation obj)
        {
            IsBusy = false;
        }

        private RelayCommand _refreshCommand;
        public RelayCommand RefreshCommand
        {
            get
            {
                return _refreshCommand
                    ?? (_refreshCommand = new RelayCommand(Init));
            }
        }

        /*public RelayCommand<object> BeforeSaveCommand { get; private set; }

        private void BeforeSave(object obj)
        {
            if (_dc.HasChanges && !_dc.IsSubmitting)
                _dc.SubmitChanges();
        }*/
    }
}